package com.wtpaint.lib.zhttp.chain;

import com.wtpaint.lib.zhttp.Call;
import com.wtpaint.lib.zhttp.Response;

import java.io.IOException;
import java.util.List;

public class InterceptorChain {
    private List<Interceptor> interceptorList;
    private int index;
    private Call call;

    public InterceptorChain(List<Interceptor> interceptorList, int index, Call call) {
        this.interceptorList = interceptorList;
        this.index = index;
        this.call = call;
    }

    public Response process() throws IOException {
        if (index >= interceptorList.size()) {
            throw new IOException("index out of range");
        }
        Interceptor interceptor = interceptorList.get(index);
        InterceptorChain next = new InterceptorChain(interceptorList, index + 1, call);
        return interceptor.intercept(next);

    }
}
